package com.weijian.demo1.mapper;

import com.weijian.demo1.entity.Team;
import com.weijian.demo1.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TeamMapper {

    @Insert("INSERT INTO teams (name, description, owner_id, created_at, updated_at) " +
            "VALUES (#{name}, #{description}, #{ownerId}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Team team);

    @Select("SELECT t.* FROM teams t " +
            "JOIN team_members tm ON t.id = tm.team_id " +
            "WHERE tm.user_id = #{userId}")
    List<Team> findByUserId(Long userId);

    @Select("SELECT u.* FROM users u " +
            "JOIN team_members tm ON u.id = tm.user_id " +
            "WHERE tm.team_id = #{teamId}")
    List<User> findMembersByTeamId(Long teamId);

    @Select("SELECT * FROM teams")
    List<Team> findAll();
}